<template>
  <a-modal
    title="新增订单"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-form-item
          label="所属发布"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select style="width: 100%" placeholder="请选择所属发布" v-decorator="['managerId',{rules: [{ required: true, message: '请选择所属发布！' }]}]" >
            <a-select-option v-for="(item,index) in pushManage" :key="index" :value="item.id" @click="selectedValue(item.id)">{{ item.lineName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="订单编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input style="width: 100%" placeholder="请输入订单编号" v-decorator="['orderNumber', {rules: [{required: true, message: '请输入订单编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="订单状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select allow-clear placeholder="请选择订单状态" >
            <a-select-option v-for="(item,index) in tempInfo" :key="index" :value="item.id" @click="selecteOrderStatus(item.dictName)">{{ item.dictName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="投放时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker placeholder="请选择监播日期" @change="selectDate" style="width: 100%" v-decorator="['putTime', {rules: [{required: true, message: '请选择监播日期！'}]}]" />
        </a-form-item>
        <a-form-item
          label="终端数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input-number style="width: 100%" placeholder="请输入终端数量" v-decorator="['terminalNum', {rules: [{required: true, message: '请输入终端数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="城市"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input style="width: 100%" placeholder="请输入城市" v-decorator="['city', {rules: [{required: true, message: '请输入城市！'}]}]" />
        </a-form-item>
        <a-form-item
          label="开始播放时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker placeholder="请输入开始播放时间" @change="selectStartPutDate" style="width: 100%" v-decorator="['playStartTime', {rules: [{required: true, message: '请输入开始播放时间！'}]}]" />
        </a-form-item>
        <a-form-item
          label="结束播放时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker placeholder="请输入结束播放时间" @change="selectEndPutDate" style="width: 100%" v-decorator="['playEndTime', {rules: [{required: true, message: '请输入结束播放时间！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { pushSearchAdd } from '@/api/modular/system/pushSearchManage'
  import { pushManageList } from '@/api/modular/system/pushManageManage'
  import { getChildInfo } from '@/api/modular/system/fenDictManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        managerId: '',
        byManagerId: '',
        pushManage: [],
        putTime: '',
        playStartTime: '',
        dictName: '',
        tempInfo: {},
        playEndTime: ''
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.managerId = record
        this.visible = true
        getChildInfo({ 'dictCode': 'DDZT' }).then(res => {
          this.tempInfo = res.data
        })
        this.getManageList()
      },
      getManageList() {
        pushManageList().then((res) => {
          this.pushManage = res.data
        })
      },
      selectedValue(value) {
        this.byManagerId = value
        console.log('selected : ', value)
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        if (this.byManagerId == null) {
          this.$message.error('请选择所属发布')
          return false
        }
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            if (this.putTime != null && this.putTime !== '') {
              values['putTime'] = this.putTime
            }
            if (this.playStartTime != null && this.playStartTime !== '') {
              values['playStartTime'] = this.playStartTime
            }
            if (this.playEndTime != null && this.playEndTime !== '') {
              values['playEndTime'] = this.playEndTime
            }
            values['managerId'] = this.byManagerId
            values['orderStatus'] = this.dictName
            console.log('values.managerId : ', values)
            pushSearchAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      selecteOrderStatus(dictName) {
        this.dictName = dictName
      },
      selectDate(date, dateString) {
        this.putTime = dateString
        console.log('putTime : ', dateString)
      },
      selectStartPutDate(date, dateString) {
        this.playStartTime = dateString
        console.log('playTime : ', dateString)
      },
      selectEndPutDate(date, dateString) {
        this.playEndTime = dateString
        console.log('playTime : ', dateString)
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
